<?php

namespace App\Http\Controllers\Api\Cart;

use DB;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Service\CartService;

use App\Models\Mall\MallMaster;

use App\Models\ConfigureFunction;

class CartController extends Controller
{

    /**
     * 加入购物车（普通商品）
     * @param Request $request
     * @return array
     */
    public function addGeneral(Request $request)
    {
        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $mallID = $request->input('mallID', 0);
        $postID = $request->input('postID');
        $goods_amount = $request->input('goods_amount', 1);

        if ( !ebsig_is_int($postID) ){
            return response()->json(['code' => 100001, 'message' => '参数错误']);
        }

        if ( !ebsig_is_int($goods_amount) ) {
            $goods_amount = 1;
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            $pcustID = 0;
        }

        $is_choose_goods_by_mall = ConfigureFunction::getConfigureField(8, 'is_choose_goods_by_mall');
        if ($is_choose_goods_by_mall) {

            if (!ebsig_is_int($mallID)) {
                return response()->json(['code' => 100003, 'message' => '请先选择门店']);
            }

            //查询门店信息
            $mall = MallMaster::getCache($mallID);
            if (!$mall) {
                return response()->json(['code' => 100004, 'message' => '门店不存在']);
            }

        }

        $args_data = array(
            'mallID' => $mallID,
            'postID'  => $postID,
            'goods_amount'  => $goods_amount,
            'pcustID'  => $pcustID,
            'session_id'  => $session_id,
            'cart_name'  => 'shop_cart',
            'platform'  => 0,
            'auto_code'  => null
        );
        $result_array = CartService::addGeneral($args_data);

        if ($result_array['code'] == 200) {
            return response()->json(['code' => 200, 'message' => 'ok', 'data' => $result_array['data']]);
        } else {
            return response()->json(['code' => $result_array['code'], 'message' => $result_array['message']]);
        }

    }


    /**
     * 立即购买
     * @param Request $request
     * @return array
     */
    public function nowBuy(Request $request)
    {

        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $mallID = $request->input('mallID', 0);
        $postID = $request->input('postID');
        $goods_amount = $request->input('goods_amount', 1);

        if ( !ebsig_is_int($postID) ){
            return response()->json(['code' => 100001, 'message' => '参数错误']);
        }

        if ( !ebsig_is_int($goods_amount) ) {
            $goods_amount = 1;
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 100002, 'message' => '请登录']);
        }

        $is_choose_goods_by_mall = ConfigureFunction::getConfigureField(8, 'is_choose_goods_by_mall');
        if ($is_choose_goods_by_mall) {

            if (!ebsig_is_int($mallID)) {
                return response()->json(['code' => 100003, 'message' => '请先选择门店']);
            }

            //查询门店信息
            $mall = MallMaster::getCache($mallID);
            if (!$mall) {
                return response()->json(['code' => 100004, 'message' => '门店不存在']);
            }

        }

        $args_data = array(
            'mallID' => $mallID,
            'postID'  => $postID,
            'goods_amount'  => $goods_amount,
            'pcustID'  => $pcustID,
            'cart_name'  => 'now_buy',
            'platform'  => $platform,
            'auto_code'  => ''
        );
        $result_array = CartService::nowBuy($args_data);

        if ($result_array['code'] == 200) {
            return response()->json(['code' => 200, 'message' => 'ok', 'data' => $result_array['data']]);
        } else {
            return response()->json(['code' => $result_array['code'], 'message' => $result_array['message']]);
        }

    }

    /**
     * 初始化接口
     * @param Request $request
     * @return array
     */
    public function init(Request $request)
    {
        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $mallID = $request->input('mallID', 1);
        $pcustID = get_customer_id($session_id, $platform);

        if (!$pcustID) {
            return response()->json(['code' => 10000, 'message' => '请登录']);
        }

        $params = [
            'session_id' => $session_id,
            'mallID' => $mallID,
            'userId' => $pcustID,
            'platform' => '0'
        ];

        //获取购物车数据
        $cart = CartService::loadCart(null, $params);
        if ($cart['code'] != 200) {
            return response()->json(['code' => 10001, 'message' => $cart['message']]);
        }

        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $cart['data']]);

    }

    //修改普通商品数量
    public function upGeneralGoods(Request $request)
    {

        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $mallID = $request->input('mallID', 1);
        $postID = $request->input('postID');
        $goods_amount = $request->input('goods_amount');

        if (!ebsig_is_int($postID)) {
            return response()->json(['code' => 10000, 'message' => '缺少参数：postID']);
        }

        if (!is_numeric($goods_amount)) {
            return response()->json(['code' => 10000, 'message' => '缺少参数：goods_amount']);
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 10000, 'message' => '请登录']);
        }

        $args_data = array(
            'mallID' => $mallID,
            'postID'  => $postID,
            'goods_amount'  => $goods_amount,
            'pcustID'  => $pcustID,
            'platform'  => '0',
            'session_id' => $session_id
        );

        //修改购物车商品数量
        $result_array = CartService::addGeneral($args_data);
        if ($result_array['code'] != 200) {
            return response()->json(['code' => $result_array['code'], 'message' => $result_array['message']]);
        }

        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $result_array['data']]);

    }

    //删除普通商品
    public function delGeneralGoods(Request $request)
    {
        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $postID = $request->input('postID');
        if ( !ebsig_is_int($postID) ) {
            return response()->json(['code' => 10000, 'message' => '缺少参数：postID']);
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 10002, 'message' => '请登录']);
        }

        $result_array = CartService::delGeneralGoods( $postID, $pcustID, $session_id );

        return response()->json(['code' => $result_array['code'], 'message' => $result_array['message']]);

    }

    //选择或取消购物车商品
    public function isBuy(Request $request)
    {
        $id = $request->input('id');
        $sign = $request->input('sign');
        $type = $request->input('type');
        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);

        if( !is_numeric($sign) ){
            return response()->json(['code' => 10001, 'message' => '缺少参数：sign']);
        }

        if ( empty($type) || !in_array($type, array('general', 'group', 'points', 'abroad')) ) {
            $type = 'general';
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 10002, 'message' => '请登录']);
        }

        $bill_array = CartService::getCartInfo('shop_cart', $pcustID);

        //参数：0 => 全取消/全选
        if (isset($bill_array['goods']['general'])) {
            foreach($bill_array['goods']['general'] as $key=>$val) {
                if ($type == 'general') {
                    if ($id == 0 || in_array($key, $id) ) {
                        $val['is_buy'] = $sign;
                    }
                }

                $bill_array['goods']['general'][$key] = $val;
            }
        }

        if (isset($bill_array['goods']['group'])) {
            foreach($bill_array['goods']['group'] as $key=>$val) {
                if ($id == 0 || in_array($key, $id)) {
                    $val['is_buy'] = 1;
                }
                $bill_array['goods']['group'][$key] = $val;
            }
        }

        if (isset($bill_array['goods']['points'])) {
            foreach($bill_array['goods']['points'] as $key=>$val) {
                if ($type == 'points') {
                    if ($id == 0 || in_array($key, $id)) {
                        $val['is_buy'] = $sign;
                    }
                    $bill_array['goods']['points'][$key] = $val;
                }
            }
        }

        CartService::setCartInfo($bill_array, 'shop_cart', $pcustID);

        return response()->json(['code' => 200, 'message' => 'ok']);
    }

    //检查购物车
    public function check(Request $request)
    {

        $mallID = $request->input('mallID', 1);
        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 10002, 'message' => '请登录']);
        }

        $bill_array = CartService::getCartInfo('shop_cart', $pcustID);

        $result_array = CartService::checkCart($bill_array, $pcustID, $mallID, $platform);

        return response()->json(['code' => $result_array['code'], 'message' => $result_array['message']]);

    }
}
